/*****************************************************************************************
   
   Copyright (c) Microsoft Corporation. All rights reserved.

   Use of this code sample is subject to the terms of the Microsoft
   Permissive License, a copy of which should always be distributed with
   this file.  You can also access a copy of this license agreement at:
   http://www.microsoft.com/resources/sharedsource/licensingbasics/permissivelicense.mspx

 ****************************************************************************************/

using System;

namespace CommandUtils
{
	public class GrooveUtilBase : CommandUtils.UtilBase
	{
		private GrooveWebServicesV12Helpers.Context m_Context;
		
		private string m_Host;
		private string m_RequestKey;
		private string m_AccountURI;
		private string m_IdentityURI;

		private int m_TimeoutInMilliseconds;

		private bool m_SkipAccountIdentityChecks;

		private GrooveWebServicesV12Helpers.Account[] m_Accounts;
		private GrooveWebServicesV12Helpers.Account m_Account;
		private GrooveWebServicesV12Helpers.Identity[] m_Identities;
		private GrooveWebServicesV12Helpers.Identity m_Identity;

		public GrooveUtilBase(string[] i_Arguments) 
			: base(i_Arguments)
		{
		}

		public GrooveUtilBase(string[] i_Arguments, bool i_SkipAccountIdentityChecks)
			: base(i_Arguments)
		{
			m_SkipAccountIdentityChecks = i_SkipAccountIdentityChecks;
		}

		public override CheckResult CheckParameters()
		{
			CommandUtils.UtilBase.CheckResult CheckResult = base.CheckParameters();
			if (CheckResult != CommandUtils.UtilBase.CheckResult.Succeeded)
				return CheckResult;

			m_TimeoutInMilliseconds = base.GetParameterValueInt("Timeout", -1);
			
			m_Host = base.GetParameterValue("Host");

			m_RequestKey = base.GetParameterValue("RequestKey");

			if (m_TimeoutInMilliseconds != -1)
			{
				m_Context = new GrooveWebServicesV12Helpers.Context
					(
					m_RequestKey, 
					m_Host,
					m_TimeoutInMilliseconds
					);
			}
			else
			{
				m_Context = new GrooveWebServicesV12Helpers.Context
					(
					m_RequestKey, 
					m_Host
					);
			}

			m_SkipAccountIdentityChecks |= base.GetParameterValueFlag("SkipAccountIdentityChecks");
			if (m_SkipAccountIdentityChecks)
				return CommandUtils.UtilBase.CheckResult.Succeeded;

			// Always enumerate Accounts
			// If there is an Account URI specified, use that.
			// Otherwise: enumerate Accounts.  If there is more than one Account, we *need* an Account to be specified.

			m_AccountURI = base.GetParameterValue("Account");
			m_Accounts = GrooveWebServicesV12Helpers.Account.GetAccounts(m_Context);
			
			if (m_Accounts.Length == 1)
			{
				if (!System.String.IsNullOrEmpty(m_AccountURI) && (m_AccountURI != m_Accounts[0].URI))
				{
					base.StandardError.WriteLine("The specified Account was not found.");
				}
				else
				{
					m_Account = m_Accounts[0];
					m_AccountURI = m_Account.URI;
				}
			}
			else
			{
				if (System.String.IsNullOrEmpty(m_AccountURI))
				{
					base.StandardError.WriteLine("There are multiple Accounts on this device; you must specify the Account URI.");
				}
				else
				{
					foreach( GrooveWebServicesV12Helpers.Account Account in m_Accounts)
					{
						if (Account.URI == m_AccountURI)
						{
							m_Account = Account;
							break;
						}
					}

					if (m_Account == null)
					{
						base.StandardError.WriteLine("The specified Account was not found.");
					}
				}
			}

			if (m_Account == null)
			{
				base.StandardError.WriteLine("Accounts found:");
				
				foreach (GrooveWebServicesV12Helpers.Account Account in m_Accounts)
					base.StandardError.WriteLine("\t{0}\t=\t{1}", Account.URI, Account.Name);

				return CommandUtils.UtilBase.CheckResult.Failed;
			}

			// If there is an m_Identity URI specified, use that.
			// Otherwise: enumerate identities.
			// If there is more than one m_Identity in the m_Account, we *need* an m_Identity to be specified.

			m_IdentityURI = GetParameterValue("Identity");
			m_Identities = m_Account.Identities;

			if (m_Identities.Length == 1)
			{
				if (!System.String.IsNullOrEmpty(m_IdentityURI) && (m_Identities[0].URI != m_IdentityURI))
					base.StandardError.WriteLine("The specified Identity was not found.");
				else
				{
					m_Identity = m_Identities[0];
					m_IdentityURI = m_Identity.URI;
				}
			}
			else
			{
				if (System.String.IsNullOrEmpty(m_IdentityURI))
				{
					base.StandardError.WriteLine("The Account '" + m_Account.Name + "' has multiple identities; you must specify the IdentityURI.");
				}
				else
				{
					foreach (GrooveWebServicesV12Helpers.Identity Identity in m_Identities)
					{
						if (Identity.URI == m_IdentityURI)
						{
							m_Identity = Identity;
							break;
						}
					}

					if (m_Identity == null)
						base.StandardError.WriteLine("The specified Identity was not found.");
				}
			}

			if (m_Identity == null)
			{
				base.StandardError.WriteLine("Identities found in Account {0}:", m_Account.Name );
				foreach (GrooveWebServicesV12Helpers.Identity Identity in m_Identities)
					base.StandardError.WriteLine("\t{0}\t=\t{1}", Identity.URI, Identity.Name);
				
				return CommandUtils.UtilBase.CheckResult.Failed;
			}

			return CommandUtils.UtilBase.CheckResult.Succeeded;
		}

		public CheckResult CheckParameters(out string o_ErrorString)
		{
			o_ErrorString = "";

			CommandUtils.UtilBase.CheckResult CheckResult = base.CheckParameters();
			if (CheckResult != CommandUtils.UtilBase.CheckResult.Succeeded)
				return CheckResult;

			m_TimeoutInMilliseconds = base.GetParameterValueInt("Timeout", -1);
			
			m_Host = base.GetParameterValue("Host");

			m_RequestKey = base.GetParameterValue("RequestKey");

			if (m_TimeoutInMilliseconds != -1)
			{
				m_Context = new GrooveWebServicesV12Helpers.Context
					(
					m_RequestKey, 
					m_Host,
					m_TimeoutInMilliseconds
					);
			}
			else
			{
				m_Context = new GrooveWebServicesV12Helpers.Context
					(
					m_RequestKey, 
					m_Host
					);
			}

			m_SkipAccountIdentityChecks |= base.GetParameterValueFlag("SkipAccountIdentityChecks");
			if (m_SkipAccountIdentityChecks)
				return CommandUtils.UtilBase.CheckResult.Succeeded;

			// Always enumerate Accounts
			// If there is an Account URI specified, use that.
			// Otherwise: enumerate Accounts.  If there is more than one Account, we *need* an Account to be specified.

			m_AccountURI = base.GetParameterValue("Account");
			m_Accounts = GrooveWebServicesV12Helpers.Account.GetAccounts(m_Context);
			
			if (m_Accounts.Length == 1)
			{
				if (!System.String.IsNullOrEmpty(m_AccountURI) && (m_AccountURI != m_Accounts[0].URI))
				{
					o_ErrorString += "The specified Account was not found.\r\n";
				}
				else
				{
					m_Account = m_Accounts[0];
					m_AccountURI = m_Account.URI;
				}
			}
			else
			{
				if (System.String.IsNullOrEmpty(m_AccountURI))
				{
					o_ErrorString += "There are multiple Accounts on this device; you must specify the Account URI.\r\n";
				}
				else
				{
					foreach( GrooveWebServicesV12Helpers.Account Account in m_Accounts)
					{
						if (Account.URI == m_AccountURI)
						{
							m_Account = Account;
							break;
						}
					}

					if (m_Account == null)
					{
						o_ErrorString += "The specified Account was not found.\r\n";
					}
				}
			}

			if (m_Account == null)
			{
				o_ErrorString += "\r\nAccounts found:\r\n";
				
				foreach (GrooveWebServicesV12Helpers.Account Account in m_Accounts)
					o_ErrorString += "\t" + Account.URI + " = " + Account.Name + "\r\n";

				return CommandUtils.UtilBase.CheckResult.Failed;
			}

			// If there is an m_Identity URI specified, use that.
			// Otherwise: enumerate identities.
			// If there is more than one m_Identity in the m_Account, we *need* an m_Identity to be specified.

			m_IdentityURI = GetParameterValue("Identity");
			m_Identities = m_Account.Identities;

			if (m_Identities.Length == 1)
			{
				if (!System.String.IsNullOrEmpty(m_IdentityURI) && (m_Identities[0].URI != m_IdentityURI))
					o_ErrorString += "The specified Identity was not found.\r\n";
				else
				{
					m_Identity = m_Identities[0];
					m_IdentityURI = m_Identity.URI;
				}
			}
			else
			{
				if (System.String.IsNullOrEmpty(m_IdentityURI))
				{
					o_ErrorString += "The Account '" + m_Account.Name + "' has multiple identities; you must specify the IdentityURI.\r\n";
				}
				else
				{
					foreach (GrooveWebServicesV12Helpers.Identity Identity in m_Identities)
					{
						if (Identity.URI == m_IdentityURI)
						{
							m_Identity = Identity;
							break;
						}
					}

					if (m_Identity == null)
						o_ErrorString += "The specified Identity was not found.\r\n";
				}
			}

			if (m_Identity == null)
			{
				o_ErrorString += "Identities found in Account " + m_Account.Name +":\r\n";
				foreach (GrooveWebServicesV12Helpers.Identity Identity in m_Identities)
					o_ErrorString += "\t" + Identity.URI + " = " + Identity.Name + "\r\n";
				
				return CommandUtils.UtilBase.CheckResult.Failed;
			}

			return CommandUtils.UtilBase.CheckResult.Succeeded;
		}

		public string IdentityURI
		{
			get
			{
				return m_IdentityURI;
			}
		}

		public GrooveWebServicesV12Helpers.Identity Identity
		{
			get
			{
				return m_Identity;
			}
		}

		public GrooveWebServicesV12Helpers.Context Context
		{
			get
			{
				return m_Context;
			}
		}

		public string AccountURI
		{
			get
			{
				return m_AccountURI;
			}
		}

		public GrooveWebServicesV12Helpers.Account Account
		{
			get
			{
				return m_Account;
			}
		}

		public int TimeoutInMilliseconds
		{
			get
			{
				return m_TimeoutInMilliseconds;
			}
		}
	}
}
